package com.tjcd.project.library_base.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author jiangKunKun
 * @CreateDate 2023/2/17
 * @Description
 * @SpittingGrooves
 */
public class NumberUtils {

    /**
     * 使用java正则表达式去掉多余的.与0
     * @param s
     * @return
     */
    public static String subZeroAndDot(String s){
        if(s.indexOf(".") > 0){
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    /**
     * 十以内转两位数
     * @param num
     * @return
     */
    public static String turnToTwoDigitsWithinTen(String num){
        boolean numeric = isNumeric(num);
        if (numeric){
            int i = Integer.parseInt(num);
            return turnToTwoDigitsWithinTen(i);
        }else {
            return "错误参数";
        }
    }

    /**
     * 利用正则表达式判断字符串是否是数字
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if(!isNum.matches()){
            return false;
        }
        return true;
    }



    /**
     * 十以内转两位数
     * @param num
     * @return
     */
    public static String turnToTwoDigitsWithinTen(int num){
        String result = String.valueOf(num);
        switch (num) {
            case 1: result = "01";break;
            case 2: result = "02";break;
            case 3: result = "03";break;
            case 4: result = "04";break;
            case 5: result = "05";break;
            case 6: result = "06";break;
            case 7: result = "07";break;
            case 8: result = "08";break;
            case 9: result = "09";break;
            default:
               break;
        }
        return result;
    }
}
